package com.accloud.common;

import com.accloud.cloudservice.AC;
import com.accloud.utils.PreferencesUtils;
import java.security.MessageDigest;
import org.java_websocket.drafts.Draft_75;

public class ACSigner {
    public static String genSignString(long timestamp, long timeout, String nonce) {
        return String.valueOf(timeout) + String.valueOf(timestamp) + nonce + PreferencesUtils.getString(AC.context, ACConfiguration.KEY_TOKEN);
    }

    public static String genRefreshSignString(long timestamp, long timeout, String nonce) {
        return String.valueOf(timeout) + String.valueOf(timestamp) + nonce + PreferencesUtils.getString(AC.context, ACConfiguration.KEY_REFRESH_TOKEN);
    }

    public static String genSignature(String stringToSign) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(stringToSign.getBytes("UTF-8"));
            byte[] result = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int i = b & Draft_75.END_OF_FRAME;
                if (i <= 15) {
                    sb.append(0);
                }
                sb.append(Integer.toHexString(i));
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
